const app = getApp()

Page({
	data: {
		url: app.globalData.host,

		reqTimes: 0,
		reqTimesTotal: 4,
		
		noticeTitle: [],
		// current: 0,
		imageList: [],
		projectImg: '',
		projectId: null,
		newsList: [],
		newsTabImages: [],

		tabList: [],
		// tabList: ["走进基地","赛事培训","全民健身","产业创新","体育科普","赛事培训"],
		tabIndex: 0,
	},
	onLoad() {
		wx.setNavigationBarTitle({
			title: "首页"
		})
		if (wx.getUserProfile) {
			this.setData({
				canIUseGetUserProfile: true
			})
		}
		this.getBanner();
		this.getNotice();
		this.getTopProject();
		this.getCmsCategory();
	},
	// 1 获取轮播图和广告图
	getBanner(){
		wx.showLoading({title: '加载中…'});
		app.request({
			url: '/app/banner',
			method: 'GET',
			data: {
				typeId: 1
			},
			success: res => {
				this.setData({
					imageList: res.data
				})
			}
		})
		app.request({
			url: '/app/banner',
			method: 'GET',
			data: {
				typeId: 2
			},
			success: res => {
				this.setData({
					newsTabImages: res.data
				})
			}
		})
		this.askTimes();
	},
	// 2 获取信息
	getNotice(){
		app.request({
			url: '/app/notice',
			method: 'GET',
			success: res => {
				this.setData({
					noticeTitle: res.data
				})
			}
		})
		this.askTimes();
	},
	// 3 活动图
	getTopProject(){
		app.request({
			url: '/app/topProject',
			method: 'GET',
			success: res => {
				this.setData({
					projectImg: res.data.topImg,
					projectId: res.data.id
				})
			}
		})
		this.askTimes();
	},
	// 4 新闻分类
	getCmsCategory(){
		app.request({
			url: '/app/cmsCategory',
			method: 'GET',
			success: res => {
				this.setData({
					tabList: res.data
				})
				this.getCmsNews(res.data[0].id);
			}
		})
	},
	// 4 新闻列表
	getCmsNews(id){
		app.request({
			url: '/app/cmsNews',
			method: 'GET',
			data: {
				categoryId: id,
			},
			success: res => {
				this.setData({
					newsList: res.data
				})
			}
		})
		this.askTimes();
	},
	// 关闭初始 加载中
	askTimes(){
		let times = this.data.askTimes;
		if(times < this.data.reqTimesTotal){
			timse += 1;
			this.setData({
				askTimes: times
			})
		}else{
			wx.hideLoading();
		}
	},
	// 事件处理函数
	// bindViewTap() {
	// 	wx.navigateTo({
	// 		url: '../logs/logs'
	// 	})
	// },
	// 新闻详情页
	toNewsDetail(e){
		let id = e.currentTarget.dataset.id;
		let type = e.currentTarget.dataset.type;
		wx.navigateTo({
			url: 'news/news?id=' + id + '&type=' + type
		})
	},
	tabClick(e){
		wx.showLoading({title: '加载中…'});
		if(e.currentTarget.dataset.index !== this.data.tabIndex){
			this.setData({
				reqTimes: 0,
				reqTimesTotal: 1,
				tabIndex:e.currentTarget.dataset.index
			})
			this.getCmsNews(e.currentTarget.dataset.id);
		}
	},

	// 按钮跳页
	toBookList(e){
		let id = e.currentTarget.dataset.id;
		let name = '';
		if(id == 3){ name = "场地预约" }
		else if(id == 1){ name = "科技服务预约" }
		wx.navigateTo({
			url: '../book/list/list?id=' + id + '&name=' + name
		})
	},
	toProject(e){
		let id = e.currentTarget.dataset.id;
		wx.navigateTo({
			url: '../book/detail/detail?id=' + id
		})
	},
	toShop(){
		wx.showToast({
			title: "商城搭建中",
			icon: 'none',//图标，支持"success"、"loading" 
			duration: 2000,//提示的延迟时间，单位毫秒，默认：1500 
			mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false 
			success:function(){},
			fail:function(){},
			complete:function(){}
		})
	},
})
